package com.spica.spicaschool.service

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationClientOption.setLocationProtocol
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.model.LatLng
import timber.log.Timber


/**
 * 定位Service
 */
@Suppress("unused")
private const val LOCATION_SPAN = 10 * 1000 //定位的时间间隔，单位是毫秒

class LocationService : Service(), AMapLocationListener {

  private val binder = LocationBinder()

  private lateinit var recordService: RecordService

  private lateinit var locationClient: AMapLocationClient

  private var interfaceLocationed: InterfaceLocationed? = null

  override fun onBind(p0: Intent?): IBinder {
    return binder
  }

  override fun onCreate() {
    super.onCreate()
    locationClient = AMapLocationClient(this)
    initLocationOption()
    locationClient.setLocationListener(this)
    locationClient.startLocation()
  }

  //初始化定位的配置
  private fun initLocationOption() {
    val option = AMapLocationClientOption()
    option.apply {
      locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy //定位模式，高精度
      isGpsFirst = true //GPS模式优先
      httpTimeOut = 20 * 1000 //超时时间
      interval = 2000 //定位的间隔
      isNeedAddress = true //是否返回逆地理地址信息
      isOnceLocation = false //是否单次定位
      isOnceLocationLatest = false//是否等待WIFI刷新
      setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTPS)//使用的请求协议
      isSensorEnable = false //是否使用传感器
      isWifiScan = true //开始WIFI刷新
      isLocationCacheEnable = true //使用定位缓存
      geoLanguage = AMapLocationClientOption.GeoLanguage.DEFAULT
    }
    locationClient.setLocationOption(option)
    recordService = RecordServiceImpl(this)
  }


  /**
   * 用于Service的数据回传
   */
  @Suppress("unused")
  inner class LocationBinder : Binder() {

    fun getService(): LocationService {
      return this@LocationService
    }
  }

  override fun onLocationChanged(aMapLocation: AMapLocation?) {
    if (aMapLocation?.errorCode == 0) {
      val locationValue = LatLng(aMapLocation.latitude, aMapLocation.longitude)
      Timber.e("纬度信息为 ${aMapLocation.latitude}\n经度信息为${aMapLocation.longitude}")
      //未发生错误
      recordService.recordSport(locationValue, aMapLocation.locationDetail)
      interfaceLocationed?.locationed(aMapLocation)
    } else {
      //发生了错误
      Timber.e("定位发生了错误:ErrorCode:${aMapLocation?.errorCode} ErrorInfo:${aMapLocation?.errorInfo}")
    }
  }

  override fun onUnbind(intent: Intent?): Boolean {
    Timber.d("解绑定 LocationService")
    stopSelf()
    return super.onUnbind(intent)
  }

  override fun onDestroy() {
    super.onDestroy()
    locationClient.stopLocation()
    locationClient.unRegisterLocationListener(this)
    locationClient.onDestroy()
  }


  fun setInterfaceLocationed(interfaceLocationed: InterfaceLocationed) {
    this.interfaceLocationed = null
    this.interfaceLocationed = interfaceLocationed
  }

  interface InterfaceLocationed {
    fun locationed(aMapLocation: AMapLocation?)
  }
}